//
//  ChangePhoneController.m
//  SafPay
//
//  Created by 贺庆博 on 2018/6/19.
//  Copyright © 2018年 ebo. All rights reserved.
//
#import "SettingViewController.h"
#import "ChangePhoneController.h"
#import "Common.h"
@interface ChangePhoneController ()
@property (weak, nonatomic) IBOutlet UILabel *phoneLab;

@property (weak, nonatomic) IBOutlet UIButton *codeBTN;
@property (weak, nonatomic) IBOutlet UITextField *PhoneField;
@property (weak, nonatomic) IBOutlet UITextField *pwdField;

@end

@implementation ChangePhoneController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.isBind?@"修改绑定手机":@"绑定手机";
    self.codeBTN.layer.cornerRadius = 3;
    self.codeBTN.layer.borderWidth = 1;
    self.codeBTN.layer.borderColor = rgb(33,177,227).CGColor;

    if (_isBind&&!self.isSecond) {
        self.PhoneField.text = [AccountTools shared].currentAccount.phoneNumber;
        self.PhoneField.enabled = NO;
        self.phoneLab.text = @"旧手机号";
    }
    if (_isBind&&self.isSecond) {
        self.phoneLab.text = @"新手机号";
    }
    if (!_isBind) {
        self.phoneLab.text = @"手机号";
    }
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)sendCode:(id)sender {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    NSString *path;
    if (!self.isBind) {
        [dic setValue:self.PhoneField.text forKey:@"mobile"];
        path = kSetURL(kGetCode);
    }else
    {
        [dic setValue:self.PhoneField.text forKey:@"mobile"];
        [dic setValue:[AccountTools shared].currentAccount.token forKey:@"token"];
        [dic setValue:[AccountTools shared].currentAccount.userId forKey:@"uid"];
        if (!self.isSecond) {
            path = kSetURL(kGetOldCode);
        }else
        {
            [dic setValue:self.PhoneField.text forKey:@"mobile"];
            path = kSetURL(kGetNewCode);
        }
    }



    [[TRRequest request] Post:path parameters:dic success:^(TRRequest *request, NSDictionary *responseDict) {
        
        if ([[responseDict valueForKey:@"error"] integerValue] == 0) {
            [Common countDown:self.codeBTN countdown:60];
        }else
        {
            [MBProgressHUD showErrorWithText:[responseDict valueForKey:@"error_msg"]];
        }
    } failure:^(TRRequest *request, NSError *error) {
        
        [MBProgressHUD showErrorWithText:@"网络请求失败！"];
    }];
}
- (IBAction)sureAction:(id)sender {
    if (_PhoneField.text.length < 11||_pwdField.text.length == 0) {
        [MBProgressHUD showSuccessWithText:@"请填写正确信息"];
        return;
    }
    
    if (self.isBind&&!self.isSecond) {
        ChangePhoneController *vc = [[ChangePhoneController alloc]initWithNibName:@"ChangePhoneController" bundle:nil];
        vc.isBind= YES;
        vc.isSecond = YES;
        vc.oldCode = self.pwdField.text;
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }

    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:self.PhoneField.text forKey:@"mobile"];
    [dic setValue:[AccountTools shared].currentAccount.token forKey:@"token"];
    [dic setValue:[AccountTools shared].currentAccount.userId forKey:@"uid"];
    if (!self.isBind) {
        [dic setValue:self.pwdField.text forKey:@"code"];

    }else
    {
        [dic setValue:self.oldCode forKey:@"old_code"];
        [dic setValue:self.pwdField.text forKey:@"new_code"];
    }


    [[TRRequest request] Post:kSetURL(!self.isBind?kBindPhone:kChangePhone) parameters:dic success:^(TRRequest *request, NSDictionary *responseDict) {
        
        if ([[responseDict valueForKey:@"error"] integerValue] == 0) {
            [MBProgressHUD showSuccessWithText:@"绑定成功"];
            Account *acc = [AccountTools shared].currentAccount;
            acc.phoneNumber = self.PhoneField.text;
            [[AccountTools shared] updateUserInfo:acc];
            for (UIViewController *controller in self.navigationController.viewControllers) {
                if ([controller isKindOfClass:[SettingViewController class]]) {
                    SettingViewController *revise =(SettingViewController *)controller;
                    [self.navigationController popToViewController:revise animated:YES];
                }
            }
        }else
        {
            [MBProgressHUD showErrorWithText:[responseDict valueForKey:@"error_msg"]];
        }
    } failure:^(TRRequest *request, NSError *error) {
        
        
        [MBProgressHUD showErrorWithText:@"网络请求失败！"];
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
